<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="../js/babel.js"></script>
</head>

<body>
  <div id="root"></div>
</body>

</html>

<script type="text/babel">
  // 阻止默认行为 必须显示的使用e.preventDefault 不能通过return false 阻止
  function Test(props) {
    function handleClick(e) {
      e.preventDefault()
      console.log('this is error');
    }

    return (<div>
      <a href="#" onClick={handleClick}>请你点击</a>
    </div>)
  }

  // 切换按钮
  class ToggleBtn extends React.Component {
    constructor(props) {
      super(props)
      this.state = { isToggleStatus: false }

      this.handleToggleClick = this.handleToggleClick.bind(this)
    }

    handleToggleClick() {
      this.setState({
        isToggleStatus: !this.state.isToggleStatus
      })
    }

    render() {
      return (
        <div>
          <button onClick={this.handleToggleClick}>{this.state.isToggleStatus ? 'ON' : 'OFF'}</button>
        </div>
      )
    }
  }

  // 事件处理程序注意点：this绑定的问题的解决方法
  class TestFunc extends React.Component {
    constructor(props) {
      super(props)
      this.state = {}
    }

    handleThisQustion = () => {
      console.log('this', this);
    }

    render() {
      return (
        <div>
          <a href="#" onClick={this.handleThisQustion}>click me</a>
        </div>
      )
    }
  }

  class TestFunc2 extends React.Component {
    handleThisQustion() {
      console.log('this', this);
    }

    render() {
      return <button onClick={() => this.handleThisQustion()}>click me</button>
    }
  }

  // 向事件处理程序传递参数
  class StudyProps extends React.Component {

    handleProps(a, e) {
      console.log('props', a, e);
    }

    render() {
      return <div>
        <button onClick={(e) => this.handleProps(1, e)}>click me</button>
        <button onClick={this.handleProps.bind(this, 2)}>click me2</button>
      </div>
    }
  }

  ReactDOM.render(<StudyProps />, document.getElementById('root'))
</script>